package org.example.yygh.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.yygh.common.exception.YyghException;
import org.example.yygh.common.result.ResultCodeEnum;
import org.example.yygh.hosp.mapper.HospitalSetMapper;
import org.example.yygh.hosp.service.IHospitalSetService;
import org.example.yygh.model.hosp.HospitalSet;
import org.example.yygh.vo.hosp.HospitalQueryVo;
import org.example.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author hu
 * @version 1.0
 * @date 2021/3/13 0013 下午 3:21
 * @description
 */
@Service
@Transactional(readOnly = true)
public class IHospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper,HospitalSet> implements IHospitalSetService {

    private static  final  int  HOSPITAL_NORMAL_STATUS = 0;

    private static  final  int  HOSPITAL_DELETE_STATUS = 1;

    @Override
    public List<HospitalSet> listsHospitalSet() {
        LambdaQueryWrapper<HospitalSet> query = new LambdaQueryWrapper<>();
        query.eq(HospitalSet::getIsDeleted, HOSPITAL_NORMAL_STATUS);
        return baseMapper.selectList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void softDeleteHospitalById(Long id) {
        HospitalSet hospitalSet = this.findNormalById(id);
        if (hospitalSet == null) {
            throw  new YyghException( ResultCodeEnum.PARAM_ERROR.getMessage(), ResultCodeEnum.PARAM_ERROR.getCode());
        }

        hospitalSet.setIsDeleted(HOSPITAL_DELETE_STATUS);
        baseMapper.updateById(hospitalSet);
    }

    @Override
    public Page<HospitalSet> findPageByHospitalQueryVo(int current, int pageSize, HospitalSetQueryVo hospitalQueryVo) {

        Page<HospitalSet> pageParams = new Page<>(current, pageSize);
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalQueryVo.getHoscode())) {
            queryWrapper.eq(HospitalSet::getHoscode, hospitalQueryVo.getHoscode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())) {
            queryWrapper.eq(HospitalSet::getHosname, hospitalQueryVo.getHosname());
        }

        return baseMapper.selectPage(pageParams, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveHospitalSet(HospitalSet hospitalSet) {
        if (this.findNormalByHospitalName(hospitalSet.getContactsName()) != null) {
            throw new YyghException("该医院名称已存在", -1);
        }
        if (this.findNormalByHospitalCode(hospitalSet.getHoscode()) != null) {
            throw new YyghException("该医院编码已存在", -1);
        }

        baseMapper.insert(hospitalSet);
    }

    @Override
    public HospitalSet findDetailById(Long id) {
        HospitalSet hospitalSet = baseMapper.selectById(id);
        hospitalSet.getIsDeleted().equals(HOSPITAL_DELETE_STATUS);
        return  hospitalSet.getIsDeleted().equals(HOSPITAL_DELETE_STATUS) ? null : hospitalSet;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(HospitalSet hospitalSet) {
        if (this.findNormalById(hospitalSet.getId()) == null) {
            throw new YyghException("当前医院设置不存在或者已被删除！", -1);
        }

        HospitalSet normalByHospitalCode = this.findNormalByHospitalCode(hospitalSet.getHoscode());
        if (normalByHospitalCode != null && !normalByHospitalCode.getId().equals(hospitalSet.getId())) {
            throw new YyghException("当前医院编码已存在！", -1);
        }

        HospitalSet normalByHospitalName = this.findNormalByHospitalName(hospitalSet.getHosname());

        if (normalByHospitalName != null && !normalByHospitalCode.getId().equals(hospitalSet.getId())) {
            throw new YyghException("当前医院名称已存在！", -1);
        }

        baseMapper.updateById(hospitalSet);
    }

    /**
     * 根据id 查询医院设置详情
     * @param id
     * @return
     */
    public HospitalSet findNormalById(Long id) {
        LambdaQueryWrapper<HospitalSet> query = new LambdaQueryWrapper<>();
        query.eq(HospitalSet::getId, id).eq(HospitalSet::getIsDeleted, HOSPITAL_NORMAL_STATUS);
        return baseMapper.selectOne(query);
    }


    /**
     * 根据名称查询 医院设置
     * @param hospitalName
     * @return
     */
    public HospitalSet findNormalByHospitalName(String hospitalName) {
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HospitalSet::getHosname , hospitalName)
                    .eq(HospitalSet::getIsDeleted, HOSPITAL_NORMAL_STATUS);
        List<HospitalSet> hospitalSetList = baseMapper.selectList(queryWrapper);
        return CollectionUtils.isEmpty(hospitalSetList) ? null  : hospitalSetList.get(0);
    }

    /**
     * 根据code查询 医院设置
     * @param code
     * @return
     */
    public HospitalSet findNormalByHospitalCode(String code) {
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HospitalSet::getHoscode , code)
                .eq(HospitalSet::getIsDeleted, HOSPITAL_NORMAL_STATUS);
        List<HospitalSet> hospitalSetList = baseMapper.selectList(queryWrapper);
        return CollectionUtils.isEmpty(hospitalSetList) ? null  : hospitalSetList.get(0);
    }


}
